/*
* 机架测试
* 舞者无罪
* 2014.12.02
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "sys.h"
#include "com.h"
#include "mhlabel.h"
#include "rack.h"

#define MASTER_SIGN_CAPTION_WIDTH     140
#define MASTER_SIGN_CAPTION_HEIGHT    24
#define NEEDLE_ZERO_NAME_WIDTH        60
#define RACK_CAPTION_INTER             2
#define MASTER_SIGN_CAPTION_LEFT   (CX_EDGE_LEFT * 2)
#define MASTER_SIGN_CAPTION_TOP    (CY_TITLE4_HEIGHT + CX_EDGE_LEFT)

#define RACK_SIGNAL_NUM      34
/*
 *
 */
static void CreateRackInterface(HWND hWnd)
{
    //sys btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    //left
    CreateWindow (MH_LABEL_NAME,
        MW_MAST_WARN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_MAST_READY_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 1,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_WARN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 2,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_READY_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 3,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_QUICK_HANDLE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 4,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_STOP_HANDLE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 5,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_SLOW_HANDLE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 6,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_SCRAM_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 7,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_LEFT_FORBID_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 8,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_RGHT_FORBID_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 9,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_FORBID_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 10,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_ZERO_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 11,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_LEFT_GRUB_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 12,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_RGHT_GRUB_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 13,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //mid
    CreateWindow (MH_LABEL_NAME,
        MW_YARN_BREAK_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_YARN_KNOT_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 1,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_BIG_KNOT_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 2,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_DROP_WARN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 3,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_FRNT_SHAKE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 4,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_BACK_SHAKE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 5,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_NEEDLE_ZERO_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 6,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_MAST_CODE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 7,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_CODE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 8,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_KEY_CODE_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 9,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_LEFT_BACK_DOOR_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 10,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_RGHT_BACK_DOOR_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 11,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_FRNT_RAB_WARN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 12,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_BACK_RAB_WARN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 13,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //right
    CreateWindow (MH_LABEL_NAME,
        MW_ROLL_WARN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_ROLL_BAD_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 1,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_RAB_OPEN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 2,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_ROLL_OPEN_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 3,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_LEFT_FEEDER_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 4,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);

    CreateWindow (MH_LABEL_NAME,
        MW_RGHT_FEEDER_SIGN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + MASTER_SIGN_CAPTION_LEFT,
        MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * 5,
        MASTER_SIGN_CAPTION_WIDTH,
        MASTER_SIGN_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //edit
    int i = 0, j = 0;
    int nn[3] = {14, 14, RACK_SIGNAL_NUM - 28};
    for(i = 0; i < 3; ++i)
    {
        for(j = 0; j < nn[i]; ++j)
        {
            CreateWindow (MH_LABEL_NAME,
                "1",
                WS_CHILD | WS_VISIBLE,
                IDC_EDIT1 + i * 14 + j,
                CX_SCREEN * i / 3 + MASTER_SIGN_CAPTION_LEFT + MASTER_SIGN_CAPTION_WIDTH,
                MASTER_SIGN_CAPTION_TOP + (MASTER_SIGN_CAPTION_HEIGHT + RACK_CAPTION_INTER) * j,
                NEEDLE_ZERO_NAME_WIDTH,
                MASTER_SIGN_CAPTION_HEIGHT,
                hWnd, MH_LABEL_BOOL_TYPE);
        }
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_ESC:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
}
/*
 * update signal of rack
 */
static void UpdateRackSignal(void *arg)
{
    HWND hWnd = (HWND)arg;
    PWND_RACK p = (PWND_RACK)GetWindowAdditionalData(hWnd);
    while(p->rack_query_state)
    {
        RACK_SIGNAL signal;
        short needle = 0;
        if(!GetRackSignal(g_knit_data, &signal, &needle))
        {
            int i = 0;
            int signal_index[RACK_SIGNAL_NUM] = {2, 40, 3, 40, 12, 10, 11, 30,
                14, 15, 21, 20, 16, 17, 8, 9, 40, 34, 0, 1, 25, 40, 40, 40,
                19, 13, 24, 24, 26, 26, 35, 40, 6, 7
            };
            uchar *q = (uchar*)&signal;
            for(i = 0; i < RACK_SIGNAL_NUM; ++i)
            {
                HWND hWndTmp = GetDlgItem(hWnd, IDC_EDIT1 + i);
                if(hWndTmp)
                {
                    const char *sc = GetWindowCaption(hWndTmp);
                    uchar n = atoi(sc);
                    n = !n;
                    if(n != q[signal_index[i]])
                    {
                        SetWindowCaption(hWndTmp, q[signal_index[i]] ? "0" : "1");
                        InvalidateRect(hWndTmp, NULL, TRUE);
                    }
                }
            }
        }
        else
        {
            //通讯出错
        }
        usleep(40000);
    }
    pthread_exit(NULL);
}
/*
*
*/
static int RackWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateRackInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            PWND_RACK p = (PWND_RACK)GetWindowAdditionalData(hWnd);
            if(p)
            {
                if(!p->rack_query_state)
                {
                    SwitchToKnitState(g_knit_data, FALSE);
                    p->rack_query_state = TRUE;
                    if(pthread_create(&p->rack_thread_id, NULL, (void*)UpdateRackSignal, (void*)hWnd))
                    {
                        printf("failed to create rack signal thread\n");
                        p->rack_query_state = FALSE;
                    }
                }
            }
        }
        break;
    case MSG_CLOSE:
        {
            PWND_RACK p = (PWND_RACK) GetWindowAdditionalData(hWnd);
            if(p)
            {
                if(p->rack_query_state)
                {
                    p->rack_query_state = FALSE;
                 //   pthread_join(p->rack_thread_id, NULL);
                }
                free(p);
            }
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_XTCS]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateRackWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_XTCS_WND_BMP) / 2;
    int y = (CY_SCREEN - CY_XTCS_WND_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_INPUT_TEST_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = RackWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_XTCS_WND_BMP;
    CreateInfo.by = y + CY_XTCS_WND_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = (int)calloc(1, sizeof(WND_RACK));
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
